package com.danielmiller.mygradebook;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class EditClassOverview extends Activity implements OnItemClickListener{

	EditText name, weight;
	public int class_index;
	private ListView listView;
	ArrayList<String> arrayList = new ArrayList<String>();
	String array[] = null;
	SQLiteDatabase db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gradingrubric);
		
		class_index = getIntent().getIntExtra("CLASS_INDEX", -1);

		//Populate ListView
		db = openOrCreateDatabase(
				"SEMESTER_DB.db"
				, SQLiteDatabase.CREATE_IF_NECESSARY
				, null
		);
		db.setVersion(1);
		db.setLocale(Locale.getDefault());
		db.setLockingEnabled(true);
		
		Cursor cur = db.rawQuery("SELECT category_name FROM tbl_category WHERE" +
				"category_class_id = '" + class_index + "'", null);

		if (cur != null ) {
			if  (cur.moveToFirst()) {
				do {
					String catName = cur.getString(cur.getColumnIndex("category_name"));
					arrayList.add(catName);
				}while (cur.moveToNext());
			}
		}
		array = arrayList.toArray(array);
		cur.close();
		db.close();

		listView=(ListView)findViewById(R.id.grading_rubric_list_view);
		listView.setOnItemClickListener(this);
		listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , 
				array));
		
		final Button createButton = (Button) findViewById(R.id.add_new_section_button);
		createButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(EditClassOverview.this, EditClass.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("CLASS_INDEX", class_index);
				startActivity(intent);
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		int category_index = 0;
		
		Toast.makeText(this, "u clicked " + array[position] ,Toast.LENGTH_LONG).show();
		
		db = openOrCreateDatabase(
				"SEMESTER_DB.db"
				, SQLiteDatabase.CREATE_IF_NECESSARY
				, null
		);
		db.setVersion(1);
		db.setLocale(Locale.getDefault());
		db.setLockingEnabled(true);
		
		Cursor cur = db.rawQuery("SELECT category_id FROM tbl_category WHERE" +
				"category_name = '" + array[position] + "'", null);

		if (cur != null ) {
			 cur.moveToPosition(0);
			 category_index = Integer.parseInt(cur.getString(1));
			 db.close();
			 cur.close();
		}
		
		
		Intent intent = new Intent(EditClassOverview.this, AddGrade.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("CLASS_INDEX", class_index);
		intent.putExtra("CATEGORY_INDEX", category_index);
		startActivity(intent);
	}
}
